package com.decent.task.config;

import cn.hutool.core.util.StrUtil;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * xxl-job 配置
 *
 * @author wangyan
 **/
@Configuration
@PropertySource(value = "classpath:xxl-config.properties")
@Getter
@Component
public class XxlJobConfig {
    /**
     * 服务器地址
     */
    @Value("${server-address}")
    private String serverAddress;
    /**
     * 服务器接入token
     */
    @Value("${access-token}")
    private String accessToken;
    /**
     * 执行器名称
     */
    @Value("${executor-name}")
    private String executorName;
    /**
     * 日志路径
     */
    @Value("${log-path}")
    private String logPath;
    /**
     * 日志保存时间
     */
    @Value("${log-retention-days}")
    private String logRetentionDays;
    /**
     * 定时任务服务虚拟端口
     */
    @Value("${port}")
    private String port;
    /**
     * 定时任务服务ip
     */
    @Value("${ip}")
    private String ip;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(serverAddress);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setAppname(executorName);
        if (StrUtil.isNotBlank(ip)) {
            xxlJobSpringExecutor.setIp(ip);
        }
        xxlJobSpringExecutor.setPort(Integer.parseInt(port));
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(Integer.parseInt(logRetentionDays));
        return xxlJobSpringExecutor;
    }
}
